package com.sxf.utils;

import cn.hutool.core.date.DateUtil;
import com.sxf.biz.customer.dao.SequenceMapper;
import com.sxf.consts.CodeTypeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.text.DateFormat;
import java.text.NumberFormat;
import java.util.Date;
import java.util.Objects;

/**
 * @author
 * 生成编号
 */
@Component
public class CodeUtils {

    private static final String REDIS_SELF_GROWING_KEY = "station:self:Growing:key";

    private static final long TIMEOUT = 86400L;

    @Autowired
    SequenceMapper sequenceMapper;


    /**
     * 获取编号
     * @param type 适用于单一类型
     * @return
     */
    public String getCode(CodeTypeEnum type) {
        if (Objects.isNull(type)) {
            return null;
        }
        synchronized (type){
            Integer number = sequenceMapper.getNextSequence(type.getCode());
            if(number > 10000){
                number = number%10000;
            }
            String code = String.format("%05d", number);

            return type.getCode()+ "-" + DateUtil.format(new Date(),"yy") + code;
        }
    }
    
    private String geFourNumber(long number, int digit){
		NumberFormat formatter = NumberFormat.getNumberInstance();   
		formatter.setMinimumIntegerDigits(digit);   
		formatter.setGroupingUsed(false); 
		return formatter.format(number);
	}
}
